﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ShopManagement
{
    public partial class FormEdit : Form
    {
        private readonly ShopServices services = new ShopServices();
        private int id;
        public FormEdit()
        {
            InitializeComponent();
        }
        public FormEdit(int id)
        {
            InitializeComponent();
            var item = services.GetById(id);
            if (item != null)
            {
                textName.Text = item.Name;
                textDsc.Text = item.Description;
               textPrice.Text = item.Price.ToString();
                textStock.Text = item.Stock.ToString();
                textCat.Text = item.Category;
                this.id = id;
            }
        }
        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void butOk_Click(object sender, EventArgs e)
        {
            ProductsEditModel model = new ProductsEditModel()
            {
                ProductID= this.id,
                Name = textName.Text,
               Description = textDsc.Text,
                Price = decimal.Parse(textPrice.Text, CultureInfo.InvariantCulture), // 确保使用正确的文化信息
                Stock = int.Parse(textStock.Text),
                Category = textCat.Text,
            };
            services.Edit(model);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
